package com.baizhi.service.impl;

import com.baizhi.dao.UserMapper;
import com.baizhi.dto.PageDTO;
import com.baizhi.entity.User;
import com.baizhi.entity.UserExample;
import com.baizhi.service.UserService;
import com.baizhi.vo.CommonResultPageVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@Transactional  //是声明式事务管理 编程中使用的注解
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    //根据页码，大小查询数据
    @Override
    public CommonResultPageVO queryAllPage(PageDTO pageDTO) {

        //创建条件对象
        UserExample example = new UserExample();
        example.createCriteria().andDeleteTimeIsNull();  //删除时间为null

        //查询总条数
        int total = userMapper.selectCountByExample(example);

        //创建分页对象  参数(limit)：起始条数,查询条数
        RowBounds rowBounds = new RowBounds((pageDTO.getPage()-1)*pageDTO.getPageSize(),pageDTO.getPageSize());
        /**
         *  page  1  pageSize  2
         *
         *  1  0~1
         *  2  2~3
         *  3  4~5
         *     （page-1）*pageSize
         * */
        //分页查询数据  参数：条件对象,分页对象
        List<User> feedbackList = userMapper.selectByExampleAndRowBounds(example, rowBounds);

        //遍历集合
        for (User user : feedbackList) {
            //获取用户id
            Integer id = user.getId();
            //根据用户id查询学分
            user.setScore(88);
        }

        /*map.put("page",pageDTO.getPage());  //当前页
        map.put("total",total);  //总条数  3
        map.put("rows",feedbackList);  //分页的数据  2*/

        CommonResultPageVO commonResultPageVO = new CommonResultPageVO(pageDTO.getPage(), total, feedbackList);

        return commonResultPageVO;
    }


    //更新
    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    //添加
    @Override
    public void add(User user) {

        user.setUpdateTime(user.getCreateTime());

        userMapper.insertSelective(user);
    }


    //文件上传
    @Resource
    HttpServletRequest request;

    @Override
    public String uploadAvatarUrl(MultipartFile avatarUrl) {

        //使用云存储存储文件

        //1.获取文件名
        String filename = avatarUrl.getOriginalFilename();

        String newName=new Date().getTime()+"-"+filename;

        //2.获取文件路径  根据相对路径获取绝对路径
        String realPath = request.getSession().getServletContext().getRealPath("/upload/avatar");

        //创建文件夹
        File file = new File(realPath);
        if(!file.exists()){
            file.mkdirs();
        }

        //3.文件上传
        try {
            avatarUrl.transferTo(new File(realPath,newName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return newName;
    }


    //根据id查询
    @Override
    public User queryById(Integer id) {

        UserExample example = new UserExample();
        example.createCriteria().andIdEqualTo(id);

        User user = userMapper.selectOneByExample(example);

        return user;
    }


    //删除一条
    @Override
    public void delete(User user) {

        user.setDeleteTime(new Date());

        userMapper.updateByPrimaryKeySelective(user);
    }


}
